/*
 * CS 567 GUI - Semester Project - "DecisionLists"
 * Jason Frank
 * Spring 2011
 *
 * This File:  Container for the main center view of the app.  It replaces the center panel (upon
 *             the user changing views) with the desired new view.  I am implementing this container
 *             as a regular view so that it is notified of the desired view change.  Hopefully I'll
 *             implement fading from the old view to the new view to enhance the visual cue of the
 *             changed view.
 *
 *             The main app class only instantiates this class, rather than all possible views that
 *             can occupy the center area. Therefore, this class instantiates all of the center-area views.
 */

package Decision_Lists;


import java.awt.Dimension;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JPanel;

//import javax.swing.border.Border;

/*
 */
public class CenterPanel3 extends Panel_Fader_User implements DL_ViewInterface  {

  boolean test_fade = false;
  boolean test_change_views = false;
  final String view_name = "CenterPanel";
  boolean is_center_view = false;
  DL_Model model;
  int panelWidthPref  = 1000;
  int panelHeightPref = 600;
  static DL_ViewInterface current_center_view;
  DL_ViewInterface butterfly;
  DL_ViewInterface spreadsheet;
  //DL_ViewInterface addItemView;
  DL_ViewInterface addItemForm;
  DL_ViewInterface gridView;

  DL_ViewInterface initialView; // must be set after the actual view has been instantiated
  // This map holds a ref to all views. The class can use this map to find a ref to the new desired view
  //  when the user selects a different view from the drop-down menu.
  // Making this map static b/c the combobox in HeaderView needs to know which view
  //  corresponds to the string title of the view, so that it can call model.setCurView().
                // Name,   View
  static TreeMap<String, DL_ViewInterface> views_map = new TreeMap<String, DL_ViewInterface>();


  JPanel newView;  // used in fade timers
  JPanel oldView;

  /////////// END OBJECT VARS ///////////////////
  ///////////////////////////////////////////////
  
  ///////////////////
  // Constructor
  public CenterPanel3(DL_Model model, DL_ViewInterface addItemForm) throws Exception  {


    this.model = model;           // link the model to this view
    this.model.addObserver(this, this.view_name, is_center_view); // link this view to the model

    // INITIAL LAYOUT
    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    setBorder(BorderFactory.createEmptyBorder(0,15,20,15));  // top,left,bottom,right

    // INSTANTIATE ALL CENTER VIEWS and add them to map
    butterfly = new ButterflyView2(model);
    views_map.put(butterfly.getViewName(), butterfly);
    spreadsheet = new SpreadsheetView(model);
    views_map.put(spreadsheet.getViewName(), spreadsheet);
 //   addItemView = new AddItemView(model);
 //   views_map.put(addItemView.getViewName(), addItemView);
    this.addItemForm = addItemForm;
    views_map.put(addItemForm.getViewName(), this.addItemForm);
    this.gridView = new GridView(model);
    views_map.put(gridView.getViewName(), gridView);

    // Set the CURRENT VIEW in this center panel
        //  current_center_view = butterfly;
    current_center_view = spreadsheet;

    // Must also set the current view in the model
         //initialView = butterfly;
    initialView = spreadsheet;
    model.setInitialView(initialView);

    // Setup the panel-fader stuff by user superclass
    super.init_PanelFaderUser(this, (JPanel)current_center_view); // args: target_panel, current_panel;
  } ///// end constructor /////


  /////////////////////////////////////////////////
  ////// Methods Required by the Interface ////////

  // The Model calls this to notify this View of a changed current view
  // This method removes the previous center view and adds the new center view.
  public void curViewChanged(String old_view_name, String new_view_name ) {

    /////////////////////////
    //// START FADES   //////

    newView = (JPanel) views_map.get(new_view_name);
    oldView = (JPanel) current_center_view;
    // Use superclass's Panel_Fader3 obj to handle fading out oldview and fading in newview
    pf3.setFadein_panel(newView);
    // If fading to/from AddItemForm, decrease the fade time/chunk for performance reasons
    //  (AddItemForm's group layout might be slowing things down on computer's with crappy graphics card)   
    int savedFadeinInterval = pf3.getFadein_interval_miliseconds();
    float savedFadeinDelta  = pf3.getFadein_delta();

    if(((DL_ViewInterface)newView) == addItemForm ) {
      System.out.println("\n\nCenter_Panel detected a fade to AddItemForm;  setting fade params for faster fade\n\n");

      int fadeInterval = pf3.getFadein_interval_miliseconds() / 2;
      float fadeDelta    = pf3.getFadein_delta() * 2;
      pf3.setFadein_interval_miliseconds(fadeInterval);
      pf3.setFadein_delta(fadeDelta);
      //pf3.resetFadeinHandler(); // trying to get the ActionListener obj to grab new runtime vals of pf3 obj
    }

    // Start the fade
    try {
      pf3.startFade();
    } catch (Exception ex) {
      Logger.getLogger(CenterPanel3.class.getName()).log(Level.SEVERE, null, ex);
    }
    // Reset the fade params
    // NOTE: I think this resets the vals too quickly since the timer animation is prob still going on
    //        but I also tried commenting this out, and then the fadein handler seems to try to
    //        run the code with the new fadein vals and the old fadein vals.  :(  For now I'm just leaving it.
    if(((DL_ViewInterface)newView) == addItemForm ) {

      pf3.setFadein_interval_miliseconds(savedFadeinInterval);
      pf3.setFadein_delta(savedFadeinDelta);
    }


  } ///// end curViewChanged() //////////

  // Model calls this to inform views of an added (new) list item.
  //  The views will then probably need to re-layout their list items to reflect the change.
  public void itemAdded(List_Item_Data newItem) {
    // Do nothing
  }  
  // Model calls this to inform views of a changed list item (i.e., new item added, old item deleted, etc).
  //  The views will then probably need to re-layout their list items to reflect the change.
  public void itemChanged() {
    // Do nothing, this view doesn't care about new list items
  }

  public String getViewName() {
    return this.view_name;
  } //// end getViewName() /////////

  /////// end required by interface functions  //////
  ///////////////////////////////////////////////////


  static DL_ViewInterface getCurrentCenterView() {
    return current_center_view;
  }



  @Override
  //public Dimension getMinimumSize()   { return new Dimension(panelWidthPref -250, panelHeightPref -250); }
  public Dimension getMinimumSize()   { return new Dimension(panelWidthPref, panelHeightPref); }
  @Override  // TODO: set the width as the width of the parent
  public Dimension getPreferredSize() { return new Dimension(panelWidthPref +370, panelHeightPref +280); }

} //////////////////// end class CenterPanel //////////////////

